package cn.wolfcode.luowowo.search.service.impl;

import cn.wolfcode.luowowo.search.reporsitory.IUserInfoSearchRepository;
import cn.wolfcode.luowowo.search.service.IUserInfoSearchService;
import cn.wolfcode.luowowo.search.templates.UserInfoTemplate;
import com.alibaba.dubbo.config.annotation.Service;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

@Service
public class UserInfoSearchServiceImpl implements IUserInfoSearchService{
    @Autowired
    private IUserInfoSearchRepository dao;
    @Autowired
    private ElasticsearchTemplate template;
    @Autowired
    private TransportClient client;
    //创建索引
    public void createIndex() {
        template.createIndex(UserInfoTemplate.class);
        template.putMapping(UserInfoTemplate.class);
    }

    public void initData() {
        
    }

    public void save(UserInfoTemplate tt) {
        dao.save(tt);
    }

    public Page<UserInfoTemplate> query(String name) {
        BoolQueryBuilder query = QueryBuilders.boolQuery();

        query.must(QueryBuilders.termQuery("destName",name));

        return dao.search(query, PageRequest.of(0,5));
    }
}
